package com.secret.manager.service.impl;

import com.secret.core.constant.MinioConstant;
import com.secret.core.constant.RedisConstant;
import com.secret.dao.dto.PictureDownloadDto;
import com.secret.dao.entity.UserControls;
import com.secret.dao.entity.UserLogin;
import com.secret.dao.mapper.PictureListMapper;
import com.secret.manager.minio.MinioUtils;
import com.secret.manager.redis.RedisUtils;
import com.secret.manager.service.UserSelectService;
import io.swagger.annotations.ApiModelProperty;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;
/**
 redis相关内容还需要设置过期时间定时执行
 **/
@Service
public class UserSelectServiceImpl implements UserSelectService {
    @Resource
    private PictureListMapper pictureListMapper;
    @Resource
    private RedisUtils  redisUtils;
    @Resource
    private MinioUtils minioUtils;
    @Resource
    private PictureDownloadDto pictureDownloadDto;


    @ApiModelProperty("判断用户积分")
    @Override
    public Boolean judgmentUser(UserLogin userLogin, List<String>pictureNameList) {
        return userLogin.getUserWangbi() >= pictureNameList.size() * 2;
    }
    @ApiModelProperty("获取redis中的图片链接")
    @Override
    public List<PictureDownloadDto> redisSearch(String number) {
        List<Object> list=redisUtils.lGet(number,0,-1);
        List<PictureDownloadDto> pictureDownloadDtoList=new ArrayList<>();
        for (Object obj:list){
            pictureDownloadDtoList.add(pictureDownloadDto.getClass().cast(obj));
        }
        return pictureDownloadDtoList;
    }
    @ApiModelProperty("mysql中查询图片返回文件夹地址")
    @Override
    public List<PictureDownloadDto> mysqlSearch(String number) {
        List<String> list=pictureListMapper.selectByNumber(number);
        List<PictureDownloadDto> urlList=new ArrayList<>();
        for (String str:list){
            PictureDownloadDto pictureDownloadDto=new PictureDownloadDto();
            pictureDownloadDto.setPictureName(str);
            pictureDownloadDto.setPictureUrl(number+"/"+str);
            urlList.add(pictureDownloadDto);
        }
        return urlList;
    }

    @ApiModelProperty("用户记录操作redis")
    @Override
    public Boolean databaseInsert(UserLogin userLogin, List<String> nameLists, int TypeId) {
        Boolean a=true;
        Timestamp now = new Timestamp(System.currentTimeMillis());

        for (int i=0;i<nameLists.size();i++){
            UserControls userControls=new UserControls();
            userControls.setPictureName(nameLists.get(i));
            userControls.setUserOpenId(userLogin.getUserOpenId());
            userControls.setControlsTypeId(TypeId);
            userControls.setCreateTime(now);
            a=redisUtils.lSet(RedisConstant.USER_CONTROLS,userControls);
            if (!a){
                System.out.println(userControls);
                return false;
            }
        }
        return true;
    }

    @ApiModelProperty("插入minio外链接")
    @Override
    public List<PictureDownloadDto> minioSearch(List<PictureDownloadDto> pictureNameList) {
        String webUrl="";
        for (PictureDownloadDto pictureDownloadDto1 : pictureNameList) {
            webUrl=minioUtils.getPresignedObjectUrl(MinioConstant.BUCKET_NAME, pictureDownloadDto1.getPictureUrl(),600);
            pictureDownloadDto1.setPictureUrl(webUrl);
        }
        for (PictureDownloadDto pictureDownloadDto2 : pictureNameList) {
            System.out.println(pictureDownloadDto2);
        }
        return pictureNameList;
    }

}
